package com.nopay.dxdwphone.utils;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.SPUtils;

import java.nio.charset.Charset;

import com.nopay.dxdwphone.Constants;
import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;


/**
 * @ProjectName: safe_production
 * @Package: cn.com.dhc.safe_production.utils
 * @ClassName: RememberUitls
 * @Description: 记录用户名和密码功能
 * @Author: anqi
 * @CreateDate: 2020/9/21 17:36
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/9/21 17:36
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class RememberUitls {

    /**
     * 设置用户名
     *
     * @param userName
     * @param password
     */
    public static void setRemember(String userName, String password) {
        boolean isRemember = SPUtils.getInstance().getBoolean(Constants.SP_IS_REMEMBER, true);
        if (isRemember) {
            AES aes = new AES(Mode.CBC, Padding.ZeroPadding, Constants.AES_KEY.getBytes(), Constants.AES_KEY.getBytes());
            byte[] result = aes.encrypt(userName + Constants.SPLITE_SIGN + password, Constants.CAHRSET);
            String remember = Base64.encode(result);
            SPUtils.getInstance().put(Constants.SP_REMEMBER, remember);
        } else {
            SPUtils.getInstance().put(Constants.SP_REMEMBER, "");
        }
    }

    /**
     * 获取用户名
     *
     * @return
     */
    @NonNull
    public static String[] getRemember() {
        boolean isRemember = SPUtils.getInstance().getBoolean(Constants.SP_IS_REMEMBER, true);
        String[] split = new String[2];
        if (isRemember) {
            String remember = SPUtils.getInstance().getString(Constants.SP_REMEMBER);
            if (null != remember && remember.length() > 0) {
                byte[] decode = Base64.decode(remember);
                AES aes = new AES(Mode.CBC, Padding.ZeroPadding, Constants.AES_KEY.getBytes(), Constants.AES_KEY.getBytes());
                Charset charset = Charset.forName(Constants.CAHRSET);
                String userNamePwd = aes.decryptStr(decode, charset);
                try {
                    split = userNamePwd.split(Constants.SPLITE_SIGN);
                } catch (RuntimeException e) {

                }
            }
        } else {

        }

        return split;
    }

}
